package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping({"/", "index.html"})
    public String welcomePage(Model model) {
        // 获取一级分类所有缓存
        List<CategoryEntity> list1 = categoryService.getLevel1Category();
        model.addAttribute("categorys", list1);
        return "index";
    }

    @ResponseBody
    @RequestMapping("index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatlogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }

    /**
     * 分布式锁redisson演示
     */
    @ResponseBody
    @RequestMapping("/getLock")
    public String getLock() {
        RLock mylock = redissonClient.getLock("mylock");
        //加锁 推荐使用显示加时间的方法
        mylock.lock(30, TimeUnit.SECONDS);
        try {
            System.out.println(Thread.currentThread().getName() + "加锁成功，执行业务。。。");
            TimeUnit.SECONDS.sleep(30);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mylock.unlock();
            System.out.println(Thread.currentThread().getName() + "释放锁");
        }
        return "getLock";
    }

    @ResponseBody
    @RequestMapping("/write")
    public String write() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();

        String s = "";
        try {
            rLock.lock();
            System.out.println(Thread.currentThread().getId() + "获取写锁成功");
            s = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("writeValue", s);
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } finally {
            rLock.unlock();
            System.out.println(Thread.currentThread().getId() + "释放锁");
        }
        return s;
    }

    @ResponseBody
    @RequestMapping("/read")
    public String read() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        String s = "";
        rLock.lock();
        try {
            System.out.println(Thread.currentThread().getId() + "获取读锁成功");
            s = redisTemplate.opsForValue().get("writeValue");
        } finally {
            rLock.unlock();
            System.out.println(Thread.currentThread().getId() + "释放锁");
        }
        return s;
    }

    @ResponseBody
    @RequestMapping("/park")
    public String park() {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.trySetPermits(3);
        try {
            park.acquire();
            System.out.println(Thread.currentThread().getId() + "获取车位成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "park";
    }

    @ResponseBody
    @RequestMapping("/go")
    public String go() {
        RSemaphore park = redissonClient.getSemaphore("park");
        try {
            park.release();
            System.out.println(Thread.currentThread().getId() + "释放车位成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "go";
    }
}
